(function ($, doc) {
	
$.plusReady(function() {
	$.init();
	
	var oid = plus.webview.currentWebview().oid;
    mui("#complain").on("tap", "#submit", function() {
    	
        var user    = app.getState();
        var content = doc.getElementById('question').value;
//      var contact = doc.getElementById('contact').value;
  
        if(content == ''){
            $.alert('请详细描述你的问题和意见');
            return false;
        }
        var postdata = {
        		id: oid,
            uid: user.user_id,
            complain_desc: content,
            //data: getImageData()
        };
        console.log(JSON.stringify(postdata));
        mui.post(serverurl + 'Index/order/tradeComplain.html', postdata, function(res){
            if(res.status == 1){
            		mui.toast('提交成功！');
            		plus.webview.currentWebview().close();
            	} else {
            		mui.toast('提交失败！');
            }
        })
    })
    
    
    function getImageData() {
    		
    		var images = document.getElementsByTagName('img');
    		
    		if (!images || !images.length) {
    			return '[]';
    		}
    		
        //生成canvas
        var canvas = document.createElement('canvas');
        var context = canvas.getContext('2d');
        var list = [];
        
        for (var i = 0, l = images.length; i < l; i++) {
        		var img = images[i];
        		var width = canvas.width = 480;
        		var height = canvas.height = 480 / (img.width / img.height);
        		
        		context.drawImage(img, 0, 0, width, height);
        		
        		list.push(canvas.toDataURL('image/png', 0.5))
        }
        
        return '"' + list.join('", "') + '"';
    }


    $.plusReady(function () {
        mui(".mui-page-content").on("tap", "#uploadimg", function() {
            plus.nativeUI.actionSheet({
                title: "请选择上传方式",
                cancel: "取消",
                buttons: [ {
                    title: "相册"
                },{
                    title: "拍照"
                }]
            }, function(e) {
                if(e.index == 1) {
                    galleryMaximum();
                }else if(e.index == 2) {
                    getImage();
                }
                return true;
            })
        })

        // 从相册中选择多张图片
        function galleryMaximum(){
            // 从相册中选择图片
            console.log("从相册中选择多张图片:");
            plus.gallery.pick( function(e){
                var len = parseInt(document.getElementById('span1').getElementsByTagName('img').length);
                for(var i in e.files){
                    var img = document.createElement('img');
                    img.src = e.files[i];
                    img.style.width 	= '22%';
                    img.style.height 	= '100px';
                    img.className 		= 'img'+len;
                    img.style.paddingRight = '5px';
                    document.querySelector('#image-list .span1').appendChild(img);
                    len++;
                }
                if(len >= 4){
                    document.querySelector('#uploadimg').style.display = 'none';
                }
            }, function ( e ) {
                console.log( "取消选择图片" );
            },{filter:"image",multiple:true,maximum:1,system:false,onmaxed:function () {
                // plus.nativeUI.alert('最多只能选择1张图片');
            }});// 最多选择9张图片
        }

        function getImage() {
            var c = plus.camera.getCamera();
            c.captureImage(function(e) {
                plus.io.resolveLocalFileSystemURL(e, function(entry) {
                    var s = entry.toLocalURL() + "?version=" + new Date().getTime();
                    var len = parseInt(document.getElementById('span1').getElementsByTagName('img').length);
                    var img = document.createElement('img');
                    img.src = s;
                    img.style.width 	= '22%';
                    img.style.height 	= '100px';
                    img.className 		= 'img'+len;
                    img.style.paddingRight = '5px';
                    document.querySelector('#image-list .span1').appendChild(img);
                }, function(e) {
                    console.log("读取拍照文件错误：" + e.message);
                });
            }, function(s) {
                console.log("error" + s);
            }, {
                filename: "_doc/camera"
            })
        }
    })

})
}(mui, document));
